import { openNotificationWithIcon } from "../../utils/comm";
import Pannel from "./Pannel";

const pattern = /^[a-zA-Z][a-zA-Z0-9_]*$/;

export default class NodePannel extends Pannel {
  constructor(props) {
    super(props);
    this.openNotificationWithIcon = openNotificationWithIcon;
  }

  checkOverrideid = (ev) => {
    const {
      getNodes,
      selectedModel: { id: selectId, label: selectLabel, model },
    } = this.props;
    const OverrideidValue = ev.target.value;
    if (!pattern.test(OverrideidValue)) {
      this.openNotificationWithIcon(
        "warning",
        "",
        `${selectLabel}元素编码输入格式不正确，请输入以字母开头，且由字母、数字、下划线组成的编码`,
      );
    } else if (getNodes && getNodes instanceof Function) {
      const nodes = getNodes() || [];
      const sameOidItems = nodes.filter((item) => {
        const {
          model: { model: citemModel, shape, id },
        } = item;
        const { properties = {} } = citemModel || {};
        let status = false;
        if (
          (shape === "UserTask" || shape === "AuditTask") &&
          selectId !== id &&
          properties.overrideid === OverrideidValue
        ) {
          status = true;
        }
        return status;
      });
      if (sameOidItems.length > 0) {
        this.openNotificationWithIcon(
          "warning",
          "",
          `${selectLabel}元素编码不唯一，请重新输入`,
        );
      }
    }
    this.handleChangeProperties(
      "overrideid",
      ev.target.value,
      model,
      this.props.selectedModel.shape,
      false,
    );
  };

  // getTreeRef = (treeThis) => {
  //   this.treeRef = treeThis;
  // }

  handleformVisible = (visible = false, cb) => {
    if (this.props.disable) return false;
    this.setState({ formVisible: visible }, (cb || (() => {}))());
  };

  handleHttpCallbackVisible = (visible = false) => {
    if (this.props.disable) return false;
    this.setState({ httpcallbackFormVisible: visible }, () => {});
  };

  handleUsersSelect = (selectData, defaultNoticeChannel) => {
    const {
      selectedModel: { model, shape },
    } = this.props;
    const { properties = {} } = model || {};
    const { candidateUsers = [], noticeChannel = [] } = properties;
    const codeChannelMap = candidateUsers.reduce((map, u, i) => {
      map[u.code] = noticeChannel[i];
      return map;
    }, {});
    const newNoticeChannel = selectData.map(
      (u) => codeChannelMap[u.code] || defaultNoticeChannel,
    );
    this.handleChangeMultiProperties(
      "candidateUsers",
      {
        candidateUsers: selectData,
        noticeChannel: newNoticeChannel,
      },
      model,
      shape,
      false,
    );
  };

  handleRelationSelect = (selectData) => {
    const {
      selectedModel: { model, shape },
    } = this.props;
    this.handleChangeMultiProperties(
      "relativeRelations",
      {
        relativeRelations: selectData,
      },
      model,
      shape,
      false,
    );
  };

  handleDeleteUser = (e, item, candidateUsers) => {
    e.preventDefault();
    e.stopPropagation();
    if (this.props.disable) return false;
    let removeIdx;
    const newUsers = candidateUsers.filter((nt, idx) => {
      if (nt.code === item.code) {
        removeIdx = idx;
        return false;
      }
      return true;
    });
    const {
      selectedModel: { model, shape },
    } = this.props;
    const { properties = {} } = model || {};
    let { noticeChannel = [] } = properties;
    // 删除关联的通知渠道
    noticeChannel.splice(removeIdx, 1);
    noticeChannel = [...noticeChannel];

    this.handleChangeMultiProperties(
      "candidateUsers",
      {
        candidateUsers: newUsers,
        noticeChannel,
      },
      model,
      shape,
      false,
    );
  };

  handleDeleteUsers = (candidateUsers, deleteAll = false, selectedKeys) => {
    if (this.props.disable) return false;
    const { selectedSTAKeys } = this.state;
    const removedKeyMaps = (selectedKeys || selectedSTAKeys || []).reduce(
      (a, k) => {
        a[k] = true;
        return a;
      },
      {},
    );
    let newUsers;
    let removedIndexes = [];
    if (deleteAll) {
      newUsers = [];
      removedIndexes = candidateUsers.map((e, i) => i);
    } else {
      newUsers = candidateUsers.filter((nt, idx) => {
        if (removedKeyMaps[nt.code]) {
          removedIndexes.push(idx);
          return false;
        }
        return true;
      });
    }
    // 删除关联的通知渠道
    const {
      selectedModel: { model, shape },
    } = this.props;
    const { properties = {} } = model || {};
    let { noticeChannel = [] } = properties;
    let channel;
    removedIndexes.forEach((i) => {
      channel = noticeChannel[i];
      if (channel) {
        noticeChannel[i] = null;
      }
    });
    noticeChannel = noticeChannel.filter(Boolean);

    this.setState({ selectedSTAKeys: [] });
    this.handleChangeMultiProperties(
      "candidateUsers",
      {
        candidateUsers: newUsers,
        noticeChannel,
      },
      model,
      shape,
      false,
    );
  };

  // 删除关系
  handleDeleteRelation = (
    relativeRelations,
    deleteAll = false,
    selectedKeys,
  ) => {
    if (this.props.disable) return false;
    const { selectedRELKeys } = this.state;
    const removedKeyMaps = (selectedKeys || selectedRELKeys || []).reduce(
      (a, k) => {
        a[k] = true;
        return a;
      },
      {},
    );
    let newRelations;
    if (deleteAll) {
      newRelations = [];
    } else {
      newRelations = relativeRelations.filter((nt, idx) => {
        if (removedKeyMaps[idx]) {
          return false;
        }
        return true;
      });
    }
    const {
      selectedModel: { model, shape },
    } = this.props;

    this.setState({ selectedRELKeys: [] });
    this.handleChangeMultiProperties(
      "relativeRelations",
      {
        relativeRelations: newRelations,
      },
      model,
      shape,
      false,
    );
  };

  handleFormSelect = (selectFormData) => {
    const {
      selectedModel: { model, shape },
      updateModelForms,
    } = this.props;
    const { formKey: formkeydefinition, formName: formname, formId, formFields=[] } = selectFormData;
    this.handleChangeMultiProperties(
      "formkeydefinition",
      {
        formkeydefinition,
        formname,
        formId,
        formSource: 'innerForm',
        formFields: !formkeydefinition && !formname ? [] : formFields,
      },
      model,
      shape,
    );
    this.handleformVisible(false, updateModelForms);
  };

  handleExternalFormSelect = (selectFormData) => {
    const {
      selectedModel: { model, shape },
      updateModelForms,
    } = this.props;
    const { datas: { formKey: formkeydefinition, formName: formname, formId, formFields = [] } = {}, displayStyle, showAsUrl } = selectFormData;
    this.handleChangeMultiProperties(
      "formkeydefinition",
      {
        formkeydefinition,
        formname,
        formId,
        formSource: 'externalForm',
        displayStyle,
        showAsUrl,
        formFields: !formkeydefinition && !formname ? [] : formFields,
      },
      model,
      shape,
    );
    this.handleformVisible(false, updateModelForms);
    
  };

  handleHttpCallbackFormSelect = (selectData) => {
    const {
      selectedModel: { model, shape },
    } = this.props;
    this.handleChangeProperties("httpcallback", selectData, model, shape);
    this.handleHttpCallbackVisible(false);
  };
}
